//
//  ImageBrowerAnimator.swift
//  WBSwift
//
//  Created by maker on 2016/11/28.
//  Copyright © 2016年 maker. All rights reserved.
//  图片浏览器的动画代理

import UIKit

//面向协议开发
protocol BrowerAnimatorDelegate:NSObjectProtocol {

    func startFrame(_ indexPath: IndexPath) -> CGRect
    func endFrame() -> CGRect
    func getImageView() -> UIImageView
    
}

protocol dismissAnimatorDelegate:NSObjectProtocol {
    func getIndexpath() -> IndexPath
    func getImageview() -> UIImageView
}

class ImageBrowerAnimator: NSObject {
    var isPreseted : Bool = false
    var indexpath : IndexPath?
    
    var browerAnimatorDelegate : BrowerAnimatorDelegate?
    var dismissDelegate : dismissAnimatorDelegate?
}

//MARK:==========动画代理==========
extension ImageBrowerAnimator : UIViewControllerTransitioningDelegate{
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPreseted = true
        return self
    }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPreseted = false
        return self
    }
    
}

//MARK:==========返回转场动画的代理==========
extension ImageBrowerAnimator : UIViewControllerAnimatedTransitioning{
    //返回动画的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    //利用上下文,写动画
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        if isPreseted {
            animatedForPresent(transitionContext)
        }else{
            animateForDismissView(transitionContext)
        }
    }
    
    //弹出的动画
    func animatedForPresent(_ transitionContext: UIViewControllerContextTransitioning) {
        //通过 key 获取到要弹出的 view
        let presentView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        
        //2.把即将弹出的 view 放入到容器中
        transitionContext.containerView.addSubview(presentView)
        
        //3.准备动画
        let startFrame = browerAnimatorDelegate?.startFrame(indexpath!)
        let endFrame = browerAnimatorDelegate?.endFrame()
        let animatView = browerAnimatorDelegate?.getImageView()
        transitionContext.containerView.addSubview(animatView!)
        animatView?.frame = startFrame!
        
        //4.执行动画
        presentView.alpha = 0.0
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            animatView?.frame = endFrame!
        }, completion: { (_) in
            animatView?.removeFromSuperview()
            presentView.alpha = 1.0
            transitionContext.completeTransition(true)
           
        }) 

    }
    
    //消失的动画
    func animateForDismissView(_ transitionContext: UIViewControllerContextTransitioning) {
    
        guard let dismissDelegate = self.dismissDelegate else {
            return
        }
        // 1.取出消失的View
        let dismissView1 = transitionContext.view(forKey: UITransitionContextViewKey.from)
        dismissView1?.removeFromSuperview()
        
        let dismissView = dismissDelegate.getImageview()
        transitionContext.containerView.addSubview(dismissView)
        
        let endIndex = dismissDelegate.getIndexpath()
        let endFrame = self.browerAnimatorDelegate?.startFrame(endIndex)
    
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { 
            dismissView.frame = endFrame!
            }, completion: { (_) in
                dismissView .removeFromSuperview()
                transitionContext.completeTransition(true)
        }) 
    }
    

}





